package com.mera.tektronix.csviewer.domain



import grails.test.mixin.*
import org.junit.*

import com.mera.tektronix.csviewer.domain.Company;

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Company)
class CompanyTests {

	Company company
	
	@Before
	void setUp() {
		mockForConstraintsTests(Company)
		company = new Company(name: "Mera", email: "mera@mera.ru", address:"Nizhny Novgorod, Delovaya 13", phone: "278-88-88")
	}
	
    void testConstraintsNameUnique() {
      Company test = new Company(name: "Mera", email: "mera@mera.ru", address:"Nizhny Novgorod, Delovaya 13", phone: "278-88-88")
	  mockForConstraintsTests(Company, [test])

	  assertFalse company.validate()
	  assertEquals 'unique', company.errors['name']

	  company.name = "MFI Soft"
	  assertTrue company.validate()
	  assertNull company.errors['name']
    }
	
	void testConstraintsNameBlank() {
		company.name = ""
		assertFalse company.validate()
		assertEquals 'blank', company.errors['name']

		company.name = "Mera"
		assertTrue company.validate()
		assertNull company.errors['name']
	}
	
	void testConstraintsEmailEmail() {
		company.email = 'badEmail'
		assertFalse company.validate()
		assertEquals 'email', company.errors['email']

		company.email = 'mera@mera.ru'
		assertTrue company.validate()
		assertNull company.errors['email']
	}
		
	void testConstraintsPhoneMatches() {
		company.phone = "235678"
		assertFalse company.validate()
		assertEquals 'matches', company.errors['phone']
		
		company.phone = "278-88-65"
		assertTrue company.validate()
		assertNull company.errors['phone']
	}
}
